package com.cmf.myproject.service.system.sysparam;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cmf.myproject.dal.mybatis.mapper.SysParamMapper;
import com.cmf.myproject.dal.mybatis.model.SysParam;
import com.cmf.myproject.dal.mybatis.model.SysParamExample;
import com.cmf.myproject.dal.mybatis.model.SysParamExample.Criteria;
import com.cmf.myproject.service.system.sysparam.dto.SysParamAddItemReqDto;
import com.cmf.myproject.service.system.sysparam.dto.SysParamQueryFilter;
import com.cmf.myproject.service.system.sysparam.dto.SysParamUpdateItemReqDto;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.system.core.results.ResultBase;
import com.system.core.view.layui.table.TableViewRspDto;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class SysParamService {
	
	private SimpleDateFormat timeformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	@Autowired
	private SysParamMapper sysParamMapper;
	
	/**
	 * 页面列表查询
	 * @return TableViewRspDto 返回TableViewRspDto视图对象，符合Layui规范
	 */
	public TableViewRspDto<SysParam> sysParamList(SysParamQueryFilter filter){
		log.debug("[sysParam]查询入参：{}",filter);
		TableViewRspDto<SysParam> pageResult = new TableViewRspDto<SysParam>();
		SysParamExample example = new SysParamExample();
		Criteria criteria = example.createCriteria();
		//在此插入查询条件start
		/**
		 * jdbcType:timestamp
		 */
		try {
			if(StringUtils.isNotBlank(filter.getCreateTimeStrStart())) {
				criteria.andCreateTimeGreaterThanOrEqualTo(timeformat.parse(filter.getCreateTimeStrStart()));
			}
			if(StringUtils.isNotBlank(filter.getCreateTimeStrEnd())) {
				criteria.andCreateTimeLessThanOrEqualTo(timeformat.parse(filter.getCreateTimeStrEnd()));
			}
		} catch (ParseException e) {
			e.printStackTrace();
			pageResult.setMessage(e.getMessage());
			return pageResult;
		}
		if(filter.getId() != null) {
			criteria.andIdEqualTo(filter.getId());
		}
		if(StringUtils.isNotBlank(filter.getParamName())) {
			criteria.andParamNameEqualTo(filter.getParamName());
		}
		if(StringUtils.isNotBlank(filter.getParamValue())) {
			criteria.andParamValueEqualTo(filter.getParamValue());
		}
		if(StringUtils.isNotBlank(filter.getRemark())) {
			criteria.andRemarkEqualTo(filter.getRemark());
		}
		/**
		 * jdbcType:timestamp
		 */
		try {
			if(StringUtils.isNotBlank(filter.getUpdateTimeStrStart())) {
				criteria.andUpdateTimeGreaterThanOrEqualTo(timeformat.parse(filter.getUpdateTimeStrStart()));
			}
			if(StringUtils.isNotBlank(filter.getUpdateTimeStrEnd())) {
				criteria.andUpdateTimeLessThanOrEqualTo(timeformat.parse(filter.getUpdateTimeStrEnd()));
			}
		} catch (ParseException e) {
			e.printStackTrace();
			pageResult.setMessage(e.getMessage());
			return pageResult;
		}
		//在此插入查询条件end
		
		Page<SysParam> page = PageHelper.startPage(filter.getPage(), filter.getLimit());
		example.setOrderByClause("create_time desc");
		pageResult.setData(sysParamMapper.selectByExample(example));
		pageResult.setCount(page.getTotal());
		pageResult.setCode(0);
		pageResult.setMessage("查询成功");
		return pageResult;
	}
	
	/**
	 * 根据主键获取单条数据
	 * @param sysParam 使用sysParam对象的主键属性Id类型是Long
	 */
	public SysParam sysParamGetByPrimaryKey(SysParam sysParam) {
		SysParam result = sysParamMapper.selectByPrimaryKey(sysParam.getId());
		return result;
	}
	
	/**
	 * 根据主键更新数据
	 * @param sysParam 被更新对象（请注意，只更新有值属性）
	 */
	public ResultBase sysParamUpdateByPrimaryKey(SysParamUpdateItemReqDto sysParamUpdateItemReqDto) {
		ResultBase resultBase = new ResultBase();
		SysParam sysParam = new SysParam();
		BeanUtils.copyProperties(sysParamUpdateItemReqDto, sysParam);
		sysParamMapper.updateByPrimaryKeySelective(sysParam);
		resultBase.setSuccess(true);
		resultBase.setMessage("更新成功");
		return resultBase;
	}
	
	/**
	 * 根据主键删除数据
	 * @param keys 被删除对象主键List集合
	 */
	@Transactional
	public ResultBase sysParamDeleteByPrimaryKey(List<Long> keys) {
		ResultBase resultBase = new ResultBase();
		try {
			keys.forEach((key)->{
				sysParamMapper.deleteByPrimaryKey(key);
			});
		} catch (Exception e) {
			resultBase.setMessage(e.getMessage());
			return resultBase;
		}
		resultBase.setSuccess(true);
		resultBase.setMessage("成功删除"+keys.size()+"条记录");
		return resultBase;
	}
	
	/**
	 * 新增数据
	 * @param sysParamAddItemReqDto 新增对象
	 */
	@Transactional
	public ResultBase sysParamSave(SysParamAddItemReqDto sysParamAddItemReqDto) {
		ResultBase resultBase = new ResultBase();
		SysParam sysParam = new SysParam();
		BeanUtils.copyProperties(sysParamAddItemReqDto, sysParam);
		sysParamMapper.insertSelective(sysParam);
		resultBase.setSuccess(true);
		resultBase.setMessage("新增成功");
		return resultBase;
	}
}
